﻿namespace REVISIONMAX30
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    
    using System.Drawing;
    using System.Windows.Forms;

    public class MyGroupList : UserControl
    {
        private UIHandler _uiHandler = null;
        private ToolStripButton btnCopy;
        private ToolStripButton btnDelete;
        private ToolStripButton btnEdit;
        private ToolStripButton btnNew;
        private ToolStripButton btnRefresh;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private IContainer components = null;
        private Controls.MyListView lvGroups;
        private ToolStrip toolStrip1;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripTextBox txtName;

        public MyGroupList()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }


        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnNew = new System.Windows.Forms.ToolStripButton();
            this.txtName = new System.Windows.Forms.ToolStripTextBox();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnEdit = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.btnDelete = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnCopy = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.lvGroups = new Controls.MyListView(this.components);
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnNew,
            this.txtName,
            this.toolStripSeparator1,
            this.btnEdit,
            this.toolStripSeparator2,
            this.btnDelete,
            this.toolStripSeparator3,
            this.btnRefresh,
            this.toolStripSeparator4,
            this.btnCopy,
            this.toolStripSeparator5});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(598, 25);
            this.toolStrip1.TabIndex = 12;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnNew
            // 
            this.btnNew.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnNew.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnNew.Name = "btnNew";
            this.btnNew.Size = new System.Drawing.Size(32, 22);
            this.btnNew.Text = "New";
            this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
            // 
            // txtName
            // 
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(100, 25);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnEdit
            // 
            this.btnEdit.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(29, 22);
            this.btnEdit.Text = "Edit";
            this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // btnDelete
            // 
            this.btnDelete.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(86, 22);
            this.btnDelete.Text = "Delete Selected";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // btnRefresh
            // 
            this.btnRefresh.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(49, 22);
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
            // 
            // btnCopy
            // 
            this.btnCopy.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnCopy.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnCopy.Name = "btnCopy";
            this.btnCopy.Size = new System.Drawing.Size(36, 22);
            this.btnCopy.Text = "Copy";
            this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            this.toolStripSeparator5.Size = new System.Drawing.Size(6, 25);
            // 
            // lvGroups
            // 
            this.lvGroups.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lvGroups.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4});
            this.lvGroups.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lvGroups.FullRowSelect = true;
            this.lvGroups.GridLines = true;
            this.lvGroups.HideSelection = false;
            this.lvGroups.Location = new System.Drawing.Point(0, 25);
            this.lvGroups.Name = "lvGroups";
            this.lvGroups.Size = new System.Drawing.Size(598, 271);
            this.lvGroups.TabIndex = 14;
            this.lvGroups.UseCompatibleStateImageBehavior = false;
            this.lvGroups.View = System.Windows.Forms.View.Details;
            this.lvGroups.DoubleClick += new System.EventHandler(this.lvGroups_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Name";
            this.columnHeader1.Width = 116;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Revision";
            this.columnHeader2.Width = 55;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Created";
            this.columnHeader3.Width = 98;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "Updated";
            // 
            // MyGroupList
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lvGroups);
            this.Controls.Add(this.toolStrip1);
            this.Name = "MyGroupList";
            this.Size = new System.Drawing.Size(598, 296);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }


        private void btnCopy_Click(object sender, EventArgs e)
        {
            //if (this.lvGroups.SelectedItems.Count > 0)
            //{
            //    if (!this.txtName.Text.IsEmpty())
            //    {
            //        Group groupById = Common.GetGroupById((int) this.lvGroups.SelectedItems[0].Tag);
            //        using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //        {
            //            connection.Open();
            //            Group owner = new Group(this.txtName.Text, connection);
            //            owner.SaveOrUpdate();
            //            SortedList<int, int> list = new SortedList<int, int>();
            //            if (MessageBox.Show("Copy ConnectionString's?", "Copy ConnectionString's?", MessageBoxButtons.OKCancel) == DialogResult.OK)
            //            {
            //                foreach (ConnectionString str in groupById.ConnectionStringList)
            //                {
            //                    ConnectionString str2 = new ConnectionString(str.name, owner, connection);
            //                    str2.value = str.value;
            //                    str2.SaveOrUpdate();
            //                    list.Add(str.id, str2.id);
            //                }
            //            }
            //            if (MessageBox.Show("Copy Source's?", "Copy Source's?", MessageBoxButtons.OKCancel) == DialogResult.OK)
            //            {
            //                foreach (Source source in groupById.SourceList)
            //                {
            //                    Source source2 = new Source(source.source, owner, connection);
            //                    source2.regex = source.regex;
            //                    source2.SaveOrUpdate();
            //                }
            //            }
            //            if (MessageBox.Show("Copy SQLItem's?", "Copy SQLItem's?", MessageBoxButtons.OKCancel) == DialogResult.OK)
            //            {
            //                foreach (SQLItem item in groupById.SQLItemList)
            //                {
            //                    SQLItem item2 = new SQLItem(item.source, item.sourcetype, owner, connection);
            //                    if (list.ContainsKey(item.connectionstringid))
            //                    {
            //                        item2.connectionstringid = list[item.connectionstringid];
            //                    }
            //                    item2.index = item.index;
            //                    item2.revision = item.revision;
            //                    item2.expired = item.expired;
            //                    item2.executed = item.executed;
            //                    item2.SaveOrUpdate();
            //                }
            //            }
            //        }
            //    }
            //    else
            //    {
            //        MessageBox.Show("Specify name for new group.");
            //    }
            //}
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //List<int> idArray = new List<int>();
            //foreach (ListViewItem item in this.lvGroups.SelectedItems)
            //{
            //    idArray.Add((int) item.Tag);
            //}
            //if (idArray.Count > 0)
            //{
            //    Common.DeleteGroups(idArray);
            //}
            //this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //this.Edit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //if (!this.txtName.Text.IsEmpty())
            //{
            //    using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //    {
            //        connection.Open();
            //        this.uiHandler.ViewGroup(new Group(this.txtName.Text, connection));
            //    }
            //    this.txtName.Clear();
            //}
            //else
            //{
            //    MessageBox.Show("Specify name for new group.");
            //}
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }

        private void Edit()
        {
            //if (this.lvGroups.SelectedItems.Count > 0)
            //{
            //    this.uiHandler.ViewGroup(Common.GetGroupById((int) this.lvGroups.SelectedItems[0].Tag));
            //}
        }

        private void lvGroups_DoubleClick(object sender, EventArgs e)
        {
            //this.Edit();
        }

        public void UpdateViewer()
        {
            //this.lvGroups.Items.Clear();
            //BindingSource source = new BindingSource();
            //using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //{
            //    connection.Open();
            //    using (SQLiteCommand command = connection.CreateCommand())
            //    {
            //        command.CommandText = string.Format("select * from `groups` order by `name`", new object[0]);
            //        SQLiteDataReader reader = command.ExecuteReader();
            //        while (reader.Read())
            //        {
            //            Group group = new Group(reader);
            //            ListViewItem item = this.lvGroups.Items.Add(group.name);
            //            item.Tag = group.id;
            //            item.SubItems.Add(group.lastrevision.StringValueOrEmpty());
            //            item.SubItems.Add(group.created.StringValueOrEmpty());
            //            item.SubItems.Add(group.updated.StringValueOrEmpty());
            //        }
            //    }
            //}
            //if (this.lvGroups.Items.Count > 0)
            //{
            //    foreach (ColumnHeader header in this.lvGroups.Columns)
            //    {
            //        header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            //    }
            //}
        }

        public UIHandler uiHandler
        {
            get
            {
                return this._uiHandler;
            }
            set
            {
                this._uiHandler = value;
            }
        }
    }
}

